;
define([ 'suredyModal', ctxp + '/app/js/form-checker.js', 'jqueryForm' ], function(Modal, formChecker, File) {
	// 增加招标类型
	$('.btn.bid-type-create').on('click', function() {
		Modal.showModal({
			icon : 'icon-plus',
			title : '新建招标类型',
			uri : Suredy.contextPath + '/config/dictionary/add-form/bid-type',
			okClick : function() {
				var form = $('form.form-add-dic');

				// 校验表单
				if (!formChecker(form)) {
					return false;
				}

				form.ajaxSubmit({
					success : function(data, textStatus, jqXHR) {
						if (!data || !data.success) {
							var msg = (data && data.msg) || '增加字典项失败！';

							alert(msg);
							return false;
						}

						var dic = data.data;

						var select = $('select[name="bidType"]');
						select.append('<option value="' + dic.val + '">' + dic.val + '</option>');

						$('option', select).last().prop('selected', true);

						select.trigger('change');

						Modal.closeModal();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('增加字典项失败！\n' + errorThrown);
						return false;
					}
				});
			}
		});
	});

	// 增加招标方式
	$('.btn.bid-way-create').on('click', function() {
		Modal.showModal({
			icon : 'icon-plus',
			title : '新建招标方式',
			uri : Suredy.contextPath + '/config/dictionary/add-form/bid-way',
			okClick : function() {
				var form = $('form.form-add-dic');

				// 校验表单
				if (!formChecker(form)) {
					return false;
				}

				form.ajaxSubmit({
					success : function(data, textStatus, jqXHR) {
						if (!data || !data.success) {
							var msg = (data && data.msg) || '增加字典项失败！';

							alert(msg);
							return false;
						}

						var dic = data.data;

						var select = $('select[name="bidWay"]');
						select.append('<option value="' + dic.val + '">' + dic.val + '</option>');

						$('option', select).last().prop('selected', true);

						select.trigger('change');

						Modal.closeModal();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('增加字典项失败！\n' + errorThrown);
						return false;
					}
				});
			}
		});
	});

	// 增加招标单位
	$('.btn.bidding-create').on('click', function() {
		Modal.showModal({
			icon : 'icon-plus',
			title : '新建招标单位',
			uri : Suredy.contextPath + '/config/biddinger/create',
			okClick : function() {
				var form = $('form.form-add-biddinger');

				// 校验表单
				if (!formChecker(form)) {
					return false;
				}

				form.ajaxSubmit({
					success : function(result, textStatus, jqXHR) {
						if (!result || !result.success) {
							var msg = (result && result.msg) || '增加招标单位失败！';

							if ('exists' === msg) {
								msg = '单位名称>>>重复<<<！';
							}

							alert(msg);

							return false;
						}

						var data = result.data;

						var select = $('select[name="biddingerInfo.biddinger.id"]');
						select.append('<option value="' + data.id + '">' + data.company + '</option>');

						$('option', select).last().prop('selected', true);

						select.trigger('change');

						Modal.closeModal();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('增加招标单位失败！\n' + errorThrown);
						return false;
					}
				});
			}
		});
	});

	// 增加代理机构
	$('.btn.agency-create').on('click', function() {
		Modal.showModal({
			icon : 'icon-plus',
			title : '新建招标代理机构',
			uri : Suredy.contextPath + '/config/bid-agency/create',
			okClick : function() {
				var form = $('form.form-add-bid-agency');

				// 校验表单
				if (!formChecker(form)) {
					return false;
				}

				form.ajaxSubmit({
					success : function(result, textStatus, jqXHR) {
						if (!result || !result.success) {
							var msg = (result && result.msg) || '操作失败！';

							if ('exists' === result.msg) {
								msg = '招投标机构名称>>>重复<<<！';
							}

							alert(msg);

							return false;
						}

						var data = result.data;

						var select = $('select[name="bidAgencyInfo.agency.id"]');
						select.append('<option value="' + data.id + '">' + data.company + '</option>');

						$('option', select).last().prop('selected', true);

						select.trigger('change');

						Modal.closeModal();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('增加招标代理机构失败！\n' + errorThrown);
						return false;
					}
				});
			}
		});
	});

	// 提交招标信息
	$('a.submit-bid-info').on('click', function() {
		var form = $('form.form-bid-create');

		// 校验表单
		if (!formChecker(form)) {
			return false;
		}

		Modal.showProcessDialog({
			keyboard : false,
			text : '<div class="text-danger"><i class="icon-spinner icon-spin"></i>&nbsp;数据提交中，请稍候……</div>'
		});

		// 修复[买标书费用]、[投标保证金]的资金去向
		$('.money-source').val($('select[name="bidAgencyInfo.agency.id"] option:selected').text());

		form.ajaxSubmit({
			dataType : "json",
			success : function(data, textStatus, jqXHR) {
				if (!data || !data.success) {
					alert('提交投标信息失败！请检查编号是否重复！');
					Modal.closeProcessDialog();
					return false;
				}

				window.location.href = Suredy.contextPath + '/bid/view/' + data.data;
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('提交投标信息失败！请检查编号是否重复！\n' + errorThrown);
				Modal.closeProcessDialog();
				return false;
			}
		});
	});

	// 重置表单
	$('a.reset-form-info').on('click', function() {
		var form = $('form.form-bid-create');

		if (form.length > 0) {
			form[0].reset();
		}
	});

	// 返回浏览
	$('a.back-view').on('click', function() {
		var $this = $(this);
		var id = $this.data('id');

		window.location.href = Suredy.contextPath + '/bid/view/' + id;
	});
});
